<?php
// 7.2 不在支持费加密方法 adler32，crc32，crc32b，fnv132，fnv1a32，fnv164，fnv1a64，joaat

/**
 * @使用HMAC-SHA1算法生成oauth_signature签名值
 *
 * @param $crypt_key  密钥
 * @param $str  要处理的字符
 * @param $crypt_cat  加密方法 hash_hmac_algos()
 *
 * @return 加密结果
 */

function get_signature($str, $crypt_key, $crypt_cat = 'sha1')
{
    if (function_exists('hash_hmac')) {
        $signature = base64_encode(hash_hmac($crypt_cat, $str, $crypt_key, true));
    } else {
        $blocksize    = 64;
        $hashfunc    = 'sha1';
        if (strlen($crypt_key) > $blocksize) {
            $crypt_key = pack('H*', $hashfunc($crypt_key));
        }
        $crypt_key    = str_pad($crypt_key, $blocksize, chr(0x00));
        $ipad    = str_repeat(chr(0x36), $blocksize);
        $opad    = str_repeat(chr(0x5c), $blocksize);
        $hmac     = pack(
            'H*',
            $hashfunc(
                ($crypt_key ^ $opad) . pack(
                    'H*',
                    $hashfunc(
                        ($crypt_key ^ $ipad) . $str
                    )
                )
            )
        );
        $signature = base64_encode($hmac);
    }

    return $signature;
}

// demo
/* 
echo get_signature(json_encode([
    'uid' => 123, 'name' => 'moco'
]), 'qweqweqwe1', 'whirlpool');
echo "\n";

echo (hash_hmac('whirlpool', json_encode([
    'uid' => 123, 'name' => 'moco'
]), 'qweqweqwe1', false));
echo "\n"; */

echo password_hash('liyiting123', PASSWORD_BCRYPT,['cost' => PASSWORD_BCRYPT_DEFAULT_COST]);
echo "\n";

echo password_verify('liyiting123', '$2y$10$5pCG8Ko5qgn1R2cyu0RER.pTh8SyOqaiCLeKHl0NQ2/4k8VbzgQ0K1');
echo "\n";

//此方法竟然每次加密后的记过不一样但是 都能被verify 通过，设计有新意，于是准进源码一探究竟